﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace ToRTF
{
    class Program
    {
        static void Main(string[] args)
        {
            WordDocHandler wdh = new WordDocHandler();
            if (args.Length > 0) wdh.mainWindowTitle = args[0];
            wdh.Init();
            Stream inputStream = Console.OpenStandardInput();
            StreamReader sr = new StreamReader(inputStream);
            byte[] bytes = new byte[1000];
            
            try
            {
                while (true)
                {
                    try
                    {               
                        String input = sr.ReadLine();
                        System.Diagnostics.Debug.WriteLine(".net input==" + input + "----");
                        if (input.Equals("Q") || input.Equals( "q"))  break;
                       
                        Boolean download = false;
                        String in1 = input.Substring(0, input.IndexOf("$$$$"));
                        if (in1.StartsWith("http"))
                        {
                            WebClient wb = new WebClient();
                            NetworkCredential cred = new NetworkCredential("admin", "admin");
                            wb.Credentials = cred;
                           
                            String out2 = GetTempFileName() + in1.Substring(in1.LastIndexOf("."));

                            wb.DownloadFile(in1, out2);
                           
                            in1 = out2;
                            download = true;
                        }
                        String out1 = input.Substring(input.IndexOf("$$$$") + 4);

                        //"http://www.jfic2010.org/Instructionsforauthors2010.doc"

                        Console.WriteLine("input=" + in1 + " output=" + out1 + " ext=" + out1.Substring(out1.LastIndexOf(".")+1));

                        string s = wdh.SaveAs(in1, ref out1, out1.Substring(out1.LastIndexOf(".")+1));
                        if (download)
                        {
                          File.Delete(in1);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Got error" + e.Message);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                try
                {
                    wdh.CleanUp();
                }
                catch (Exception e)
                {
                }
            }
        }
        static string GetTempFileName()
        {
            string filename = Path.GetTempFileName();
            
            File.Delete(filename);

            return Path.GetTempPath() + "\\" + Path.GetFileNameWithoutExtension(filename);
        }
    }
}
